package com.speridian.oms.view.beans.login;


//import com.speridian.voyager.view.utils.ADFUtils;


import com.speridian.oms.view.beans.utils.ADFUtils;

import java.net.InetAddress;
import java.net.UnknownHostException;

import java.text.ParseException;

import javax.xml.ws.WebServiceRef;



//The following imports are for mirror login
/*import ws.MirrorDataService;
import ws.MirrorDataServiceSoap;
import ws.types.AuthenticateX0020TheX0020UserX0020WithX0020AD;
import ws.types.AuthenticateX0020TheX0020UserX0020WithX0020ADResponse;
import ws.types.BOUserLogin;
import ws.types.GetX0020UserX0020DetailsX0020FromX0020SessionX0020Key;
import ws.types.GetX0020UserX0020DetailsX0020FromX0020SessionX0020KeyResponse;
import ws.types.ServiceAuthenticationHeader;*/

//The following imports imlements AD Login
/*
import wsad.MirrorDataService;
import wsad.MirrorDataServiceSoap;
import wsad.types.AuthenticateX0020TheX0020UserX0020WithX0020AD;
import wsad.types.AuthenticateX0020TheX0020UserX0020WithX0020ADResponse;
import wsad.types.BOUserLogin;
import wsad.types.GetX0020UserX0020DetailsX0020FromX0020SessionX0020Key;
import wsad.types.GetX0020UserX0020DetailsX0020FromX0020SessionX0020KeyResponse;
import wsad.types.ServiceAuthenticationHeader;
*/

public class Authentication {
    @WebServiceRef
    private static wsad.MirrorDataService mirrorDataService;
    @WebServiceRef
    private static ws.MirrorDataService mirrorDataServiceDev;
    private final String serviceAuthenticationHeaderUser =
        "MirrorServiceClient";
    private final String serviceAuthenticationHeaderPasswod = "M!rr0rClient";

    public Authentication() {
        super();
    }

    public String authenticateLogin(String usename,
                                    String password) throws UnknownHostException,
                                                            ParseException {
        String adLogin =
            ADFUtils.getResourceBundleValue("com.speridian.oms.amsmodel.CustomBundle",
                                            "adLogin");
        if (adLogin.equals("true")) {

            mirrorDataService = new wsad.MirrorDataService();
            String employeeRole = "invalid";
            wsad.MirrorDataServiceSoap mirrorDataServiceSoap =
                mirrorDataService.getMirrorDataServiceSoap();

            wsad.types.ServiceAuthenticationHeader serviceAuthenticationHeader =
                new wsad.types.ServiceAuthenticationHeader();

            serviceAuthenticationHeader.setUserName(serviceAuthenticationHeaderUser);
            serviceAuthenticationHeader.setPassword(serviceAuthenticationHeaderPasswod);

            // For Mirror Login
            /*        AuthenticateX0020TheX0020User requestParameters =
            new AuthenticateX0020TheX0020User();
        requestParameters.setUserName(usename);
        requestParameters.setPassword(password);

        InetAddress inetAddress = InetAddress.getLocalHost();
        String ip = inetAddress.getHostAddress();
        requestParameters.setClientIPAddress(ip);

        AuthenticateX0020TheX0020UserResponse userResponse =
            mirrorDataServiceSoap.authenticateMirrorLogin(requestParameters,
                                                          serviceAuthenticationHeader);
//        //System.out.println("username password sent for authentication");
        BOUserLogin userLoginObj =
            userResponse.getAuthenticateX0020TheX0020UserResult();

        if (userLoginObj != null) {

//            //System.out.println("username password authenticated");
         */

            //For AD Login
            wsad.types.AuthenticateX0020TheX0020UserX0020WithX0020AD requestParameters =
                new wsad.types.AuthenticateX0020TheX0020UserX0020WithX0020AD();
            requestParameters.setUserName(usename);
            requestParameters.setPassword(password);

            InetAddress inetAddress = InetAddress.getLocalHost();
            String ip = inetAddress.getHostAddress();
            requestParameters.setClientIPAddress(ip);
            System.out.println("Step1--------------------------------------------");
            wsad.types.AuthenticateX0020TheX0020UserX0020WithX0020ADResponse userResponse =
                mirrorDataServiceSoap.authenticateADLogin(requestParameters,
                                                          serviceAuthenticationHeader);
            System.out.println("username password sent for authentication");
            wsad.types.BOUserLogin userLoginObj =
                userResponse.getAuthenticateX0020TheX0020UserX0020WithX0020ADResult();
            //System.out.println("---------fdsf---------"+userLoginObj.getEmployeeID());
            if (userLoginObj != null) {

                //            System.out.println("username password authenticated");
                wsad.types.GetX0020UserX0020DetailsX0020FromX0020SessionX0020Key sk =
                    new wsad.types.GetX0020UserX0020DetailsX0020FromX0020SessionX0020Key();
                sk.setLoginKey(userLoginObj.getSessionKey());

                sk.setClientIPAddress(ip);
                wsad.types.GetX0020UserX0020DetailsX0020FromX0020SessionX0020KeyResponse skres =
                    new wsad.types.GetX0020UserX0020DetailsX0020FromX0020SessionX0020KeyResponse();
                skres =
                        mirrorDataServiceSoap.authenticateLoginKey(sk, serviceAuthenticationHeader);

                RoleFinder role = new RoleFinder();

                if (skres != null) {
                    employeeRole = role.roleManipulator(skres);
                    //                //System.out.println("session begins");


                } else {
                    //                //System.out.println("username password not authenticated one");

                }


            } else {
                //            //System.out.println("username password not authenticated two");

                return null;
            }

            return employeeRole;
        } else {
            mirrorDataServiceDev = new ws.MirrorDataService();
            String employeeRole = "invalid";
            ws.MirrorDataServiceSoap mirrorDataServiceSoap =
                mirrorDataServiceDev.getMirrorDataServiceSoap();

            ws.types.ServiceAuthenticationHeader serviceAuthenticationHeader =
                new ws.types.ServiceAuthenticationHeader();

            serviceAuthenticationHeader.setUserName(serviceAuthenticationHeaderUser);
            serviceAuthenticationHeader.setPassword(serviceAuthenticationHeaderPasswod);

            // For Mirror Login
            /*        AuthenticateX0020TheX0020User requestParameters =
                new AuthenticateX0020TheX0020User();
            requestParameters.setUserName(usename);
            requestParameters.setPassword(password);

            InetAddress inetAddress = InetAddress.getLocalHost();
            String ip = inetAddress.getHostAddress();
            requestParameters.setClientIPAddress(ip);

            AuthenticateX0020TheX0020UserResponse userResponse =
                mirrorDataServiceSoap.authenticateMirrorLogin(requestParameters,
                                                              serviceAuthenticationHeader);
            //        //System.out.println("username password sent for authentication");
            BOUserLogin userLoginObj =
                userResponse.getAuthenticateX0020TheX0020UserResult();

            if (userLoginObj != null) {

            //            //System.out.println("username password authenticated");
             */

            //For AD Login
            ws.types.AuthenticateX0020TheX0020User requestParameters =
                new ws.types.AuthenticateX0020TheX0020User();
            requestParameters.setUserName(usename);
            requestParameters.setPassword(password);

            InetAddress inetAddress = InetAddress.getLocalHost();
            String ip = inetAddress.getHostAddress();
            requestParameters.setClientIPAddress(ip);

            ws.types.AuthenticateX0020TheX0020UserResponse userResponse =
                mirrorDataServiceSoap.authenticateMirrorLogin(requestParameters,
                                                              serviceAuthenticationHeader);
            //        //System.out.println("username password sent for authentication");
            ws.types.BOUserLogin userLoginObj =
                userResponse.getAuthenticateX0020TheX0020UserResult();

            if (userLoginObj != null) {

                //            //System.out.println("username password authenticated");


                //            System.out.println("username password authenticated");
                ws.types.GetX0020UserX0020DetailsX0020FromX0020SessionX0020Key sk =
                    new ws.types.GetX0020UserX0020DetailsX0020FromX0020SessionX0020Key();
                sk.setLoginKey(userLoginObj.getSessionKey());

                sk.setClientIPAddress(ip);
                ws.types.GetX0020UserX0020DetailsX0020FromX0020SessionX0020KeyResponse skres =
                    new ws.types.GetX0020UserX0020DetailsX0020FromX0020SessionX0020KeyResponse();
                skres =
                        mirrorDataServiceSoap.authenticateLoginKey(sk, serviceAuthenticationHeader);

                RoleFinder role = new RoleFinder();

                if (skres != null) {
                    employeeRole = role.roleManipulator(skres);
                    //                //System.out.println("session begins");


                } else {
                    //                //System.out.println("username password not authenticated one");

                }


            } else {
                //            //System.out.println("username password not authenticated two");

                return null;
            }

            return employeeRole;
        }
    }

}
